/* Just a doodle so far... */

#include <ft2build.h>
#include FT_FREETYPE_H
#include <stdio.h>
#include <stdbool.h>

typedef struct {
  bool st_intra;
  FT_Vector st_start;
} ST_Outline;

int ps_moveto (const FT_Vector * to, void * user)
{
  ST_Outline * state;
  state = (ST_Outline *) user;

  if (state->st_intra) {
    printf("closepath\n");
    printf("stroke\n");
  }
  else { 
    state->st_intra = true;
  }
  

  printf("newpath\n");
  printf( "%d %d moveto\n", 
          to->x + state->st_start.x, 
          to->y + state-> st_start.y );
  return 0;
}

int ps_lineto (const FT_Vector * to, void * user)
{
  ST_Outline * state;
  state = (ST_Outline *) user;

  printf( "%d %d lineto\n", 
          to->x + state->st_start.x, 
          to->y + state-> st_start.y );
  return 0;
}


int ps_conicto (const FT_Vector * cp1, const FT_Vector * to, void * user)
{
  ST_Outline * state;
  state = (ST_Outline *) user;

  printf( "%d %d %d %d %d %d curveto\n", 
          cp1->x + state->st_start.x, 
          cp1->y + state->st_start.y, 
          cp1->x + state->st_start.x, 
          cp1->y + state->st_start.y, 
          to->x + state->st_start.x, 
          to->y + state->st_start.y );
  return 0;
}

int ps_curveto (const FT_Vector * cp1, const FT_Vector * cp2, 
                const FT_Vector * to, void * user)
{
  ST_Outline * state;
  state = (ST_Outline *) user;

  printf( "%d %d %d %d %d %d curveto\n", 
          cp1->x + state->st_start.x, 
          cp1->y + state->st_start.y, 
          cp2->x + state->st_start.x, 
          cp2->y + state->st_start.y, 
          to->x + state->st_start.x, 
          to->y + state->st_start.y );
  return 0;
}

int main (int argc, char ** argv )
{
  FT_Error err;
  FT_Library ft_lib;
  FT_Face ft_face;
  
  FT_String * ft_str;
  FT_UInt glyph_idx;
  FT_GlyphSlot slot;

  FT_Vector cpoint;
  FT_Fixed advh;

  FT_Outline_Funcs funcs;

  ST_Outline st_decomp;

  FT_UInt i,j;

  const char * font_name;
  
  char * path_to_font;

  char test_input[30] = "Test.";

  printf("init...\n");

  if (argc !=2)
  {
    printf("Usage: freetype2 path_to_font\n");
    printf("argc = %d\n", argc) ;
    exit (1);
  }

  path_to_font = argv[1];

  printf("%% Path to font from argv: %s\n", path_to_font);


  err = FT_Init_FreeType(&ft_lib);
  fprintf(stderr, "%% err_no %d \n", err);

  //  exit(0);
  
  err = FT_New_Face(ft_lib, path_to_font, 0, &ft_face);
  fprintf(stderr, "%% err_no %d \n", err);


  font_name = FT_Get_Postscript_Name(ft_face);

  printf("%% PS name: %s\n", font_name);

  printf("%% Num faces: %d\n",   ft_face->num_faces);
  printf("%% Family name: %s\n", ft_face->family_name);

  ft_str = ft_face->style_name;


  printf("%% Style name: %s\n", ft_str);
  

  err = FT_Set_Char_Size(ft_face, 9, 0, 600, 600);
  fprintf(stderr, "%% err_no %d \n", err);


  slot = ft_face->glyph;




  glyph_idx = FT_Get_Char_Index(ft_face, 65);
  err = FT_Load_Glyph(ft_face, glyph_idx, FT_LOAD_DEFAULT);
  fprintf(stderr, "%% err_no %d \n", err);

  printf("%% Num subglyphs: %d\n", slot->num_subglyphs);
  printf("%% Num contours: %d\n", slot->outline.n_contours);
  printf("%% Num points: %d\n", slot->outline.n_points);
  printf("%% Adv x,y: %f,%f\n", (double) slot->advance.x, (double) slot->advance.y);
  printf("%% advH: %10.2f\n", (double) slot->linearHoriAdvance);
  printf("%% advV: %10.2f\n", (double) slot->linearVertAdvance);
  printf("%% TEMP: %10.2f\n", (double) (slot->outline.n_points));


  for(i=0; i < slot->outline.n_contours; i++)
  { 
    printf("%% contour start %d - point %d \n", i, slot->outline.contours[i]);
  }

  /* use FT_Outline_Decompose */

  funcs.move_to = (FT_Outline_MoveToFunc)&ps_moveto;
  funcs.line_to = (FT_Outline_LineToFunc)&ps_lineto;
  funcs.conic_to = (FT_Outline_ConicToFunc)&ps_conicto;
  funcs.cubic_to = (FT_Outline_CubicToFunc)&ps_curveto;
  funcs.shift = 0;
  funcs.delta = 0;

  st_decomp.st_intra = false;
  st_decomp.st_start.x = 0;
  st_decomp.st_start.y = 0;

  for(i=0; i < strlen(test_input); i++)
  {

    err = FT_Load_Char(ft_face, test_input[i], FT_LOAD_DEFAULT);
    fprintf(stderr, "%% err_no %d \n", err);

    err =  FT_Outline_Decompose( &(slot->outline), &funcs, &st_decomp);
    fprintf(stderr, "%% err_no %d \n", err);
  
    printf("closepath\n");
    printf("stroke\n");

    st_decomp.st_intra = false;
    st_decomp.st_start.x += slot->advance.x;
    st_decomp.st_start.y += slot->advance.y;

  }
  

  
  err = FT_Done_Face(ft_face);
  fprintf(stderr, "%% err_no %d \n", err);
 
  err = FT_Done_FreeType(ft_lib);
  fprintf(stderr, "%% err_no %d \n", err);
  
  return 0;  
}  


